package com.hospital.register.user.service.impl;

import com.hospital.register.cmn.client.DictFeignClient;
import com.hospital.register.dto.user.PatientQueryDto;
import com.hospital.register.enums.DictEnum;
import com.hospital.register.model.user.Patient;
import com.hospital.register.user.mapper.PatientMapper;
import com.hospital.register.user.service.PatientService;
import com.hospital.register.vo.cmn.DictChildVo;
import com.hospital.register.vo.user.PatientQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class PatientServiceImpl implements PatientService {

    /**
     * 访问患者数据
     */
    @Autowired
    private PatientMapper patientMapper;

    /**
     * 调用字典服务
     */
    @Autowired
    private DictFeignClient dictFeignClient;

    @Override
    public List<PatientQueryVo> selectList(PatientQueryDto conditions) {
        //查询数据
        List<PatientQueryVo> patientList = this.patientMapper.selectList(conditions);
        if (patientList == null || patientList.isEmpty()) {//结果为空
            return Collections.EMPTY_LIST;
        }

        //填充字典值
        Map<String, DictChildVo> certificatesTypeMap = this.dictFeignClient.findValueMap(DictEnum.CERTIFICATES_TYPE.getDictCode()).getData();//所有证件类型
        if (certificatesTypeMap == null) {
            certificatesTypeMap = Collections.EMPTY_MAP;
        }

        Map<String, DictChildVo> provinceMap = this.dictFeignClient.findValueMap(DictEnum.PROVINCE.getDictCode()).getData();//所有省
        if (provinceMap == null) {
            provinceMap = Collections.EMPTY_MAP;
        }

        for (PatientQueryVo patient : patientList) {
            //自己的证件类型
            String myCertificatesTypeName = "";
            DictChildVo dict = certificatesTypeMap.get(patient.getCertificatesType());
            if (dict != null) {
                myCertificatesTypeName = dict.getName();
            }

            //联系人的证件类型
            String hisCertificatesTypeName = "";
            dict = certificatesTypeMap.get(patient.getContactsCertificatesType());
            if (dict != null) {
                hisCertificatesTypeName = dict.getName();
            }

            //省
            String provinceName = "";
            dict = provinceMap.get(patient.getProvinceCode());
            if (dict != null) {
                provinceName = dict.getName();
            }

            //市、区
            String cityName = this.dictFeignClient.getName(patient.getCityCode());
            String districtName = this.dictFeignClient.getName(patient.getDistrictCode());

            //封装到单个结果中
            PatientQueryVo.OtherInfo otherInfo = new PatientQueryVo.OtherInfo();
            otherInfo.setCertificatesTypeString(myCertificatesTypeName);
            otherInfo.setContactsCertificatesTypeString(hisCertificatesTypeName);
            otherInfo.setProvinceString(provinceName);
            otherInfo.setCityString(cityName);
            otherInfo.setDistrictString(districtName);
            otherInfo.setFullAddress(provinceName + cityName + districtName + patient.getAddress());

            patient.setParam(otherInfo);
        }

        return patientList;
    }

    @Override
    public boolean insert(Patient patient) {
        return this.patientMapper.insert(patient) == 1;
    }

    @Override
    public PatientQueryVo selectMineById(Long id, Long userId) {
        PatientQueryVo patient = this.patientMapper.selectMineById(id, userId);
        if (patient == null) {
            return null;
        }

        this.fillOtherInfo(patient);
        return patient;
    }

    @Override
    public PatientQueryVo selectById(Long id) {
        PatientQueryVo patient = this.patientMapper.selectById(id);
        if (patient == null) {
            return null;
        }

        this.fillOtherInfo(patient);
        return patient;
    }

    @Override
    public boolean update(Patient patient) {
        return this.patientMapper.update(patient) == 1;
    }

    @Override
    public boolean deleteById(Long id, Long userId) {
        return this.patientMapper.deleteById(id, new Date(), userId) == 1;
    }

    /**
     * 填充就诊人的其它信息
     *
     * @param patient 就诊人
     */
    private void fillOtherInfo(PatientQueryVo patient) {
        String myCertificatesTypeName = this.dictFeignClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getCertificatesType());//自己的证件类型
        String hisCertificatesTypeName = this.dictFeignClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getContactsCertificatesType());//联系人的证件类型
        String provinceName = this.dictFeignClient.getName(DictEnum.PROVINCE.getDictCode(), patient.getProvinceCode());//省
        String cityName = this.dictFeignClient.getName(patient.getCityCode());//市
        String districtName = this.dictFeignClient.getName(patient.getDistrictCode());//区

        //封装到结果中
        PatientQueryVo.OtherInfo otherInfo = new PatientQueryVo.OtherInfo();
        otherInfo.setCertificatesTypeString(myCertificatesTypeName);
        otherInfo.setContactsCertificatesTypeString(hisCertificatesTypeName);
        otherInfo.setProvinceString(provinceName);
        otherInfo.setCityString(cityName);
        otherInfo.setDistrictString(districtName);
        otherInfo.setFullAddress(provinceName + cityName + districtName + patient.getAddress());

        patient.setParam(otherInfo);
    }

}
